package com.icetec.iceintel.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import com.icetec.iceintel.framework.IceBaseActivity;
import com.icetec.iceintel.ui.adapter.AddgroupAdapter;
import com.icetec.R;
import com.icetec.iceintel.database.DevicesListSqlHelper;
import com.icetec.iceintel.database.GroupControlSqlHelper;
import com.icetec.iceintel.model.Device;
import com.icetec.iceintel.netrequest.InstrutionConstant;
import com.icetec.iceintel.utility.ToastUtility;

/**
 * Created by amo on 2014/12/5.
 */
public class AddgroupActivity extends IceBaseActivity {
    public static final String MAC = "mac";

    ListView lightListView;
    AddgroupAdapter addgroupAdapter;
    TextView backTv;
    TextView finishTv;
    EditText nameEdtext;
    DevicesListSqlHelper devicesListSqlHelper;
    GroupControlSqlHelper groupControlSqlHelper;
    String mac = "";
    Device device = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addgroupcontrol);

        lightListView=(ListView)findViewById(R.id.activity_addgroupcontrol_lightlist_listview);
        backTv=(TextView)findViewById(R.id.activity_addgroupcontrol_back);
        finishTv=(TextView)findViewById(R.id.activity_addgroupcontrol_finish);
        nameEdtext=(EditText)findViewById(R.id.activity_addgroupcontrol_modifyinfo_edtext);

        addgroupAdapter=new AddgroupAdapter();
        devicesListSqlHelper=new DevicesListSqlHelper();
        groupControlSqlHelper=new GroupControlSqlHelper();

        lightListView.setAdapter(addgroupAdapter);

        backTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Bundle bundle = getIntent().getExtras();
        if(bundle != null && bundle.getString(MAC) != null){
            mac = bundle.getString(MAC);
            modifyGroupSet();
        }else{
            addGroupSet();
        }

    }

    /**
     * 添加群组控制
     */
    private void addGroupSet(){
        finishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Device> deviceArrayList=addgroupAdapter.getDealWithList();
                String nametext=nameEdtext.getText().toString();
                if(deviceArrayList.size()!=0&&!nametext.equals("")) {
                    int groupnum = devicesListSqlHelper.getGroupNum();
                    Device groupdevice = new Device();
                    groupdevice.setType(InstrutionConstant.DdevicesType_groupControl);
                    groupdevice.setMAC(String.valueOf(groupnum));
                    groupdevice.setName(nametext);
                    int id = devicesListSqlHelper.addSingleGroupDevice(groupdevice);
                    groupdevice.setId(id);
                    groupControlSqlHelper.saveAllDeviceReArrange(deviceArrayList,groupdevice);
                    finish();

                }

                else{
                    ToastUtility.show(R.string.no_blank_option);
                }

            }
        });
    }

    /**
     * 修改群组控制
     *
     */
    private void modifyGroupSet(){
        device = devicesListSqlHelper.getSingleDevice(mac);
        nameEdtext.setText(device.getName());
        ArrayList<Device> deviceArrayList = groupControlSqlHelper.getAllDevice(device);
        addgroupAdapter.updateChoosedDevice(deviceArrayList);
        addgroupAdapter.notifyDataSetChanged();
        finishTv.setText(getResources().getText(R.string.save));
        backTv.setText(getResources().getText(R.string.cancel));
        finishTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Device> deviceArrayList = addgroupAdapter.getDealWithList();
                if (!nameEdtext.getText().toString().equals("")) {
                    device.setName(nameEdtext.getText().toString());
                    Log.i("device: ", device.getName());
                    devicesListSqlHelper.saveSingle(device, true);
                    groupControlSqlHelper.deledeSingleDevice(device);
                    groupControlSqlHelper.saveAllDeviceReArrange(deviceArrayList, device);
                    finish();
                } else {
                    ToastUtility.show(R.string.no_blank_option);
                }
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        HideKeyboard(nameEdtext);
    }

    //隐藏虚拟键盘
    public static void HideKeyboard(View v)
    {
        InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
        if ( imm.isActive( ) ) {
            imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );

        }
    }



}
